/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ldap_api;

import org.forgerock.opendj.ldap.Connection;
import org.forgerock.opendj.ldap.DN;
import org.forgerock.opendj.ldap.ErrorResultException;
import org.forgerock.opendj.ldap.LDAPConnectionFactory;
import org.forgerock.opendj.ldap.schema.Schema;

/**
 *
 * @author anvil
 */
public class LDAPBroker {
    private static LDAPBroker lb;

    //Connection variables
    private Connection connection;
    private Schema schema;
    private String url;
    private int port;
    private String baseDN;

    private LDAPBroker() {

    }

    public static LDAPBroker getLDAPBroker() {
        if (LDAPBroker.lb == null) {
            LDAPBroker.lb = new LDAPBroker();
        }

        return LDAPBroker.lb;
    }

    public void search(String s) {
        
    }

    public void add() {
        
    }

    public void terminate() {
        
    }

    public void setConnectionDetails(String url, int port, String baseDN) {
        this.url = url;
        this.port = port;
        this.baseDN = baseDN;
    }

    public void connect() throws LDAPServerException {
        try {
            final LDAPConnectionFactory factory = new LDAPConnectionFactory(url, port);

            this.connection = factory.getConnection();
            this.schema = Schema.readSchemaForEntry(connection, DN.valueOf(this.baseDN));
        } catch (ErrorResultException e) {
            throw new LDAPServerException();
        }
    }

    public void disconnect() {
        if (connection != null) {
            connection.close();
        }
    }
}
